/*
 Copyright (c) 2011, POLIDEA
 All rights reserved.

 Redistribution and use in source and binary forms, with or without modification, 
 are permitted provided that the following conditions are met:

 Redistributions of source code must retain the above copyright notice, 
 this list of conditions and the following disclaimer.
 Redistributions in binary form must reproduce the above copyright notice, 
 this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package pl.polidea.android.utils.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewParent;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * A set of utils for controlling view basic properties.
 */
public class WidgetUtils {

    /**
     * Attach the same click listener to given views.
     * @param parentObject the parent of views (Activity/View/ViewGroup).
     * @param listener a object which implements OnClickListener interface. Can be null.
     * @param viewsRes a list of view's ids. If there is not view for given id, skip it.
     */
    public static void setClickListener(Object parentObject, OnClickListener listener, int... viewsRes) {
        for ( int i : viewsRes ) {
            View view = null;
            if ( parentObject instanceof Activity ) {
                view = ((Activity)parentObject).findViewById(i);
            } else if ( parentObject instanceof View ) {
                view = ((View)parentObject).findViewById(i);
            }
            if ( view != null ) view.setOnClickListener(listener);
        }
    }
    
    /**
     * Enable/disable given views (View::setEnabled(true/false)).
     * @param parentObject the parent of views (Activity/View/ViewGroup).
     * @param enabled a flag to set.
     * @param viewsRes a list of view's ids. If there is not view for given id, skip it.
     */
    public static void enabled(Object parentObject, boolean enabled, int... viewsRes) {
        for ( int i : viewsRes ) {
            View view = null;
            if ( parentObject instanceof Activity ) {
                view = ((Activity)parentObject).findViewById(i);
            } else if ( parentObject instanceof View ) {
                view = ((View)parentObject).findViewById(i);
            }
            if ( view != null ) view.setEnabled(enabled);
        }
    }
    
    /**
     * Change visibility of given views (View::setVisibility(VISIBLE/INVISIBLE/GONE)).
     * @param parentObject the parent of views (Activity/View/ViewGroup).
     * @param enabled new visibility value.
     * @param viewsRes a list of view's ids. If there is not view for given id, skip it.
     */
	public static void visibility(Object parentObject, int visibility, int... viewsRes) {
		for ( int i : viewsRes ) {
		    View view = null;
            if ( parentObject instanceof Activity ) {
                view = ((Activity)parentObject).findViewById(i);
            } else if ( parentObject instanceof View ) {
                view = ((View)parentObject).findViewById(i);
            } else if ( parentObject instanceof Window ) {
                view = ((Window)parentObject).findViewById(i);
            }
			if ( view != null ) view.setVisibility(visibility);
		}
	}
	
    /**
     * Change visibility of given views to GONE.
     * @param parentObject the parent of views (Activity/View/ViewGroup).
     * @param viewsRes a list of view's ids. If there is not view for given id, skip it.
     */
	public static void gone(Object parentObject, int... viewsRes) {
		visibility(parentObject, View.GONE, viewsRes);
	}
	
    /**
     * Change visibility of given views to VISIBLE.
     * @param parentObject the parent of views (Activity/View/ViewGroup).
     * @param viewsRes a list of view's ids. If there is not view for given id, skip it.
     */
	public static void visible(Object parentObject, int... viewsRes) {
		visibility(parentObject, View.VISIBLE, viewsRes);
	}
	
	
	/**
	 * Set content of TextView.
     * @param parentObject the parent of view (Activity/View/ViewGroup).
	 * @param textViewId TextView's id.
	 * @param text new text value.
	 * @return a instance of the view or null if there is not TextView object under given id.
	 */
	public static TextView setText(Object parentObject, int textViewId, CharSequence text) {
		if ( parentObject != null ) {
		    View view = null;
	        if ( parentObject instanceof Activity ) {
	            view = ((Activity)parentObject).findViewById(textViewId);
	        } else if ( parentObject instanceof View ) {
	            view = ((View)parentObject).findViewById(textViewId);
	        }
			if ( view instanceof TextView ) {
				((TextView)view).setText(text);
				return ((TextView)view);
			}
		}
		return null;
	}
	
	/**
     * Set content of TextView.
     * @param parentObject the parent of view (Activity/View/ViewGroup).
     * @param textViewId TextView's id.
     * @param resId new text value.
     * @return a instance of the view or null if there is not TextView object under given id.
     */
	public static TextView setText(Object parentObject, int textViewId, int resId) {
	    Context context = null;
	    if ( parentObject instanceof Activity ) {
	        context = (Activity)parentObject;
	    } else if ( parentObject instanceof View ) {
	        context = ((View)parentObject).getContext();
	    }
		return setText(parentObject, textViewId, context.getString(resId));
	}
	
	/**
     * Clear content of TextView.
     * @param parentObject the parent of view (Activity/View/ViewGroup).
     * @param textViewId TextView's id.
     */
	public static void clearText(Object parentObject, int textId) {
		setText(parentObject, textId, "");
	}
	
	/**
	 * Set background for given view.
     * @param parentObject the parent of view (Activity/View/ViewGroup).
	 * @param viewId view's id.
	 * @param img drawable to set.
	 */
	public static void background(Object parentObject, int viewId, Drawable img) {
		if ( parentObject != null ) {
            View view = null;
            if ( parentObject instanceof Activity ) {
                view = ((Activity)parentObject).findViewById(viewId);
            } else if ( parentObject instanceof View ) {
                view = ((View)parentObject).findViewById(viewId);
            }
            if ( view != null ) {
                view.setBackgroundDrawable(img);
            }
        }
	}
	
    /**
     * Set content of ImageView.
     * @param parentObject the parent of view (Activity/View/ViewGroup).
     * @param imgViewId view's id.
     * @param img drawable to set.
     */
	public static void setImage(Object parentObject, int imgViewId, Drawable img) {
		if ( parentObject != null ) {
		    View view = null;
            if ( parentObject instanceof Activity ) {
                view = ((Activity)parentObject).findViewById(imgViewId);
            } else if ( parentObject instanceof View ) {
                view = ((View)parentObject).findViewById(imgViewId);
            }
			if ( view instanceof ImageView ) {
				((ImageView)view).setImageDrawable(img);
			}
		}
	}
	
	/**
     * Set content of ImageView.
     * @param parentObject the parent of view (Activity/View/ViewGroup).
     * @param imgViewId view's id.
	 * @param imgResId drawable's id.
	 */
	public static void setImage(Object parentObject, int imgViewId, int imgResId) {
	    Context context = null;
        if ( parentObject instanceof Activity ) {
            context = (Activity)parentObject;
        } else if ( parentObject instanceof View ) {
            context = ((View)parentObject).getContext();
        }
		setImage(parentObject, imgViewId, context.getResources().getDrawable(imgResId));
	}
	
	/**
	 * Remove view from parent.
     * @param parentObject the parent of view (Activity/View/ViewGroup).
	 * @param viewId view's id to remove.
	 */
	public static void removeView(Object parentObject, int viewId) {
	    if ( parentObject != null ) {
            View view = null;
            if ( parentObject instanceof Activity ) {
                view = ((Activity)parentObject).findViewById(viewId);
            } else if ( parentObject instanceof View ) {
                view = ((View)parentObject).findViewById(viewId);
            }
            if ( view != null ) {
                final ViewParent parent = view.getParent();
                if ( parent instanceof ViewGroup ) {
                    final ViewGroup parentGroup = (ViewGroup)parent;
                    parentGroup.removeView(view);
                }
            }
        }        
    }
	
	
	/**
	 * Get content of TextView/EditText.
	 * @param a activity.
	 * @param editTextId text view's id.
	 * @return null if there is not view for given id. Otherwise content of TextView.
	 */
	public static String getText(Activity a, int editTextId) {
		if ( a != null ) {
			final View view = a.findViewById(editTextId);
			if ( view instanceof EditText ) {
				return ((EditText)view).getEditableText().toString();
			}
		}
		return null;
	}
	
	/**
     * Get trimmed content of TextView/EditText.
     * @param a activity.
     * @param editTextId text view's id.
     * @return null if there is not view for given id. Otherwise trimmed content of TextView.
     */
    public static String getTrimedText(Activity a, int editTextId) {
        String text = getText(a, editTextId);
        if ( text != null ) text = text.trim();
        return text;
    }
	
    /**
     * Attach adapter to ListView.
     * @param a listt views' parent.
     * @param listViewId list view's id.
     * @param adapter new adapter.
     */
	public static void setAdapter(Activity a, int listViewId, BaseAdapter adapter) {
	    if ( a != null ) {
	        final ListView listView = (ListView)a.findViewById(listViewId);
	        if ( listView != null ) {
	            listView.setCacheColorHint(0);
	            listView.setAdapter(adapter);
	        }
	    }
	}
	
}
